package org.bao.netcrawler.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.bao.netcrawler.utils.Configurer;
import org.bao.netcrawler.utils.Info;

/**
 * 
 * crawl contents from given URLs.
 * @author Bao<bb@feijiao.info>
 * @since 2010-12-24
 * @version 0.9.0
 * @license GPLv3
 */
public class Browser {
	public static final String PROPERTY_CONNECT_TIMEOUT_DEFAULT = "sun.net.client.defaultConnectTimeout";
	public static final String PROPERTY_READ_TIMEOUT_DEFAULT = "sun.net.client.defaultReadTimeout";
	
	public String getContent(String urlPath){
		Configurer config = Configurer.getInstance();
		
		System.setProperty(Browser.PROPERTY_CONNECT_TIMEOUT_DEFAULT, config.getBrowserConnectTimeout());
		System.setProperty(Browser.PROPERTY_READ_TIMEOUT_DEFAULT, config.getBrowserReadTimeout());
		
		StringBuffer sb = new StringBuffer();
		HttpURLConnection huc = null;
		BufferedReader br = null;
		
		try {
			String str = null;
			
			URL url = new URL(urlPath);
			huc = (HttpURLConnection) url.openConnection();
			
			br = new BufferedReader(new InputStreamReader(huc.getInputStream()));
			while(null != (str = br.readLine())){
				sb.append(str);
			}
			
			return sb.toString().trim();
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			huc.disconnect();
		}
		
		Info.type = Info.INFO_TYPE_NETWORK_NOT_AVAILABLE;
		return null;
	}
}
